package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.entity.User;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private UserMapper usermapper;
    /*查询当前登录用户的所有地址信息*/
    public List<AddressBook> getAllAddressBook(AddressBook addressBook) {
        List<AddressBook> list = addressBookMapper.getAllAddressBook(addressBook);
        return list;
    }

   /*新增地址*/
    public void saveAddressBook(AddressBook addressBook) {

        addressBook.setIsDefault(0);
        addressBookMapper.insertAddressBook(addressBook);
    }

    /*修改地址*/
    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBookMapper.updateAddressBook(addressBook);
    }

    /*根据id查询地址*/
    @Override
    public AddressBook getAddressBookById(Long id) {
        AddressBook addressBook = addressBookMapper.getAddressBookById(id);
        return addressBook;
    }

    /*根据id删除地址*/
    @Override
    public void deleteAddressBookById(Long id) {
        addressBookMapper.deleteAddressBookById(id);
    }

    /*设置默认地址*/
    @Override
    public void updateDefault(AddressBook addressBook) {
        // 将当前所有地址全部改为非默认地址
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateDefault(addressBook);

        // 将当前地址改为默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.updateAddressBook(addressBook);
    }

    @Override
    /*查询默认地址*/
    public AddressBook getDefaultAddress(AddressBook addressBook) {
        List<AddressBook> list  = addressBookMapper.getDefaultAddress(addressBook);
        return list.get(0);
    }
}
